package com.xm.servlet.reception_desk.index;

import com.google.gson.Gson;
import com.xm.service.reception_desk.CommodityService;
import lombok.SneakyThrows;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 导航栏
 * @author 康文程
 * @date 2024-1-2 上午 10:05
 */
@WebServlet("/navigation/bar/commodity")
public class GetNavigationBarCommodityServlet extends HttpServlet {
    private final CommodityService service = new CommodityService();

    /**
     * 导航栏查询
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=UTF-8");
        String dataString = req.getParameter("data");
        String[] data = dataString.split(",");
        System.out.println(data[0]);
        String name = "1";
        String id = "2";
        // 创建 Gson 对象
        Gson gson = new Gson();
        if (name.equals(data[0])){
            resp.getWriter().println(gson.toJson(service.searchXiaomi(data[1])));
        } else if (id.equals(data[0])) {
            resp.getWriter().println(gson.toJson(service.searchByCommodityClass(Integer.parseInt(data[1]))));
        }
    }
}
